package com.ruoyi.quartz.task;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.quartz.util.HttpUtils;
import com.ruoyi.system.domain.AppNewsDetailed;
import com.ruoyi.system.domain.AppNewsTypes;
import com.ruoyi.system.service.impl.AppNewsDetailedServiceImpl;
import com.ruoyi.system.service.impl.AppNewsTypesServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 定时抓取新闻
 *
 * @Auther sxx
 * @Date2023/3/16 10:52
 **/
@Component("collectionTask")
public class CollectionTask {

    @Autowired
    private AppNewsDetailedServiceImpl appNewsDetailedService;
    @Resource
    private AppNewsTypesServiceImpl appNewsTypesService;

    /**
     * 获取最新的新闻
     *
     * @return 最新滚动新闻的list列表  新闻类型为 1.其他
     * https://hk.ths572.monster:888/api/news/newsList?pageNum=1&pageSize=100
     */
    public String getCurrentNews() {
        URL url = null;
        String str = "";
        try {
            url = new URL(
                    "https://hk.ths572.monster:888/api/news/newsList?pageNum=1&pageSize=100"
            );

            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");
            connection.setConnectTimeout(5000);
            connection.setReadTimeout(5000);
            if (connection.getResponseCode() == 200) {
                InputStream inputStream = url.openStream();

                //网站编码
                BufferedReader reader = new BufferedReader(
                        new InputStreamReader(inputStream, "utf-8"));
                String line = "";

                StringBuilder stringBuilder = new StringBuilder();

                line = reader.readLine();
                stringBuilder.append(line);
                stringBuilder.append("\n");
                //拆解json串 取值
                str = stringBuilder.toString();

                if (str != null && !str.equals("")) {

                    JSONObject resObject = JSONObject.parseObject(str);
                    String data = resObject.getString("data");
                    JSONObject resObject2 = JSONObject.parseObject(data);
                    JSONArray list = resObject2.getJSONArray("list");

                    //遍历新闻 判断 插入数据到数据库
                    for (Object o : list) {
                        //拆解json串 取id
                        JSONObject res = JSONObject.parseObject(String.valueOf(o));
                        String id = res.getString("id");
                        //根据id 判断 ，如果数据库里有就跳过，没有就添加到数据库
                        AppNewsDetailed appNewsDetailed = appNewsDetailedService.selectAppNewsDetailedById(Long.valueOf(id));
                        if (appNewsDetailed == null) {
                            //根据新闻id获取新闻详细信息
                            AppNewsDetailed currentNewsDetailed = getCurrentNewsDetailed(id);
                            appNewsDetailedService.insertAppNewsDetailed(currentNewsDetailed);
                        }
                    }
                    reader.close();
                    inputStream.close();
                    connection.disconnect();
                }


            } else {
                return "执行成功,新闻已经全部更新完成";
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "执行失败";
    }

    /**
     * 获取详细新闻
     *
     * @return https://hk.ths572.monster:888/api/news/newsList?pageNum=1&pageSize=100
     */
    public AppNewsDetailed getCurrentNewsDetailed(String id) {
        AppNewsDetailed news = null;
        try {
            URL url = new URL(
                    "https://hk.ths572.monster:888/api/news/getNewDetails?newsId=" + id
            );
            HttpURLConnection connection = (HttpURLConnection) url
                    .openConnection();
            connection.setRequestMethod("GET");
            connection.setConnectTimeout(5000);
            connection.setReadTimeout(5000);
            System.out.println(connection.getResponseCode());
            if (connection.getResponseCode() == 200) {
                InputStream inputStream = url.openStream();

                //网站编码
                BufferedReader reader = new BufferedReader(
                        new InputStreamReader(inputStream, "utf-8"));
                String line = "";
                StringBuilder stringBuilder = new StringBuilder();

                line = reader.readLine();
                stringBuilder.append(line);
                stringBuilder.append("\n");
                String string = stringBuilder.toString();

                JSONObject resObject = JSONObject.parseObject(string);
                String data = resObject.getString("data");
                JSONObject resObject2 = JSONObject.parseObject(data);

                //获取新闻详细信息
                Long idNew = resObject2.getLong("id");
                String titleNew = resObject2.getString("title");
                String urlNew = resObject2.getString("url");
                String addTimeNew = resObject2.getString("addTime");
                addTimeNew = addTimeNew.replace("T", " ");
                String picNew = resObject2.getString("pic");
                String contentNew = resObject2.getString("content");
                String abstractNew = resObject2.getString("abstract");
                String newsIdNew = resObject2.getString("news_id");

                //赋值
                news = new AppNewsDetailed();

                news.setId(idNew);
                news.setTitle(titleNew);
                news.setUrl(urlNew);
                news.setAddTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(addTimeNew));
                news.setPic(picNew);
                news.setContent(contentNew);
                news.setAbstractNews(abstractNew);
                news.setNewsId(newsIdNew);
                news.setCreateTime(new Date());
                news.setNewsTypeName("腾讯快讯");

                reader.close();
                inputStream.close();
                connection.disconnect();
                return news;
            }

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return news;
    }

    /**
     * 获取最新的新闻
     *
     * @return 最新滚动新闻的list列表
     * http://api.tushare.pro
     */
    public String getCurrentNewsByTushare() {
        //参数 parm
        Map<String, Object> parm = new HashMap<>();
        parm.put("api_name", "major_news");
        parm.put("token", "02c63af7892cf38474327f022280ebdbe8d330f3abef5f86994a41be");
        Map<String, String> map = new HashMap<>();
        map.put("list_status", "L");
        parm.put("params", map);
        parm.put("fields", "title,pub_time,src,content");

        String nuclease = null;
        AppNewsDetailed appNewsDetailed = null;
        JSONArray jsonArray = null;
        try {
            nuclease = HttpUtils.postBody("http://api.tushare.pro", JSONObject.toJSONString(parm));
            //插接返回参数
            JSONObject resObject = JSONObject.parseObject(nuclease);
            String data = resObject.getString("data");
            JSONObject items = JSONObject.parseObject(data);
            JSONArray news = items.getJSONArray("items");

            int i = 0;
            do {
                jsonArray = news.getJSONArray(i);
                appNewsDetailed = new AppNewsDetailed();

                //随机生成id
                long l = new Random().nextInt(899999999) + 100000000;
                AppNewsDetailed anded = appNewsDetailedService.selectAppNewsDetailedById(l);
                while (true) {
                    if (anded == null) {
                        appNewsDetailed.setId(l);
                        break;
                    } else {
                        l = new Random().nextInt(899999999) + 100000000;
                        anded = appNewsDetailedService.selectAppNewsDetailedById(l);
                    }
                }
                appNewsDetailed.setTitle((String) jsonArray.get(0));

                String content = (String) jsonArray.get(1);
                content = content.replaceAll("\\(image_address=\"", "<img style=\" width: 100%\" src=\"https:").replaceAll("\"\\)", "\">");
                content = content.replaceAll("<img   src=\"", "<img style=\" width: 100%\" src=\"https:");
                content = content.replaceAll("https:http:", "https:");
                content = content.replaceAll("https:https:", "https:");
                int index = content.indexOf("关注同花顺财经（ths518）");
                if (index>=0){
                    content = content.substring(0 ,index);}

                appNewsDetailed.setContent(content);

                appNewsDetailed.setAddTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(String.valueOf(jsonArray.get(2))));
                appNewsDetailed.setCreateTime(new Date());
                appNewsDetailed.setNewsTypeName((String) jsonArray.get(3));

                //新闻类型 判断数据库是否存在此类型
                String newsType = (String) jsonArray.get(3);
                AppNewsTypes appNewsTypes = appNewsTypesService.selectAppNewsTypesByName(newsType);
                if (appNewsTypes == null) {
                    AppNewsTypes ant = new AppNewsTypes();
                    ant.setNewsTypeName(newsType);
                    ant.setCreateTime(new Date());
                    appNewsTypesService.insertAppNewsTypes(ant);
                }

                //判断数据库是否存在此新闻 否 -存数据库  是 -跳过
                AppNewsDetailed appNewsDetailed1 = appNewsDetailedService.selectByTitileAndType(appNewsDetailed.getTitle(), appNewsDetailed.getNewsTypeName());
                if (appNewsDetailed1 == null) {
                    appNewsDetailedService.insertAppNewsDetailed(appNewsDetailed);
                } else {
                    i++;
                    continue;
                }
                //请求时间条件，近一个小时
                long times = (appNewsDetailed.getCreateTime().getTime() - appNewsDetailed.getAddTime().getTime()) / 1000;
                long day1 = times / (24 * 3600);
                long hour = times % (24 * 3600) / 3600;
                long minute1 = times % 3600 / 60;
                long time = day1 * 24 * 60 + hour * 60 + minute1;
                if (time > 60) {
                    break;
                }
                i++;
            } while (i != news.size());

        } catch (IOException | ParseException e) {
            e.printStackTrace();
        }
        return "抓取完成";
    }
    //    @SneakyThrows
    public String getCurrentNewsByTushare2() {

        List<AppNewsDetailed> list = appNewsDetailedService.findList(new AppNewsDetailed());
        for (AppNewsDetailed appNewsDetailed : list) {
            if (appNewsDetailed != null) {
                if (appNewsDetailed.getContent()!=null&&!appNewsDetailed.getContent().equals("")&&!appNewsDetailed.getNewsTypeName().equals("腾讯快讯"))
                {
                    String content = appNewsDetailed.getContent();
                    content = content.replaceAll("<img src=\"https:", "<img style=\" width: 100%\" src=\"https:");
                    content = content.replaceAll("<img   src=\"https:", "<img style=\" width: 100%\" src=\"https:");
                    content = content.replaceAll("https:https:", "https:");
                    content = content.replaceAll("https:http:", "https:");
                    int index = content.indexOf("关注同花顺财经（ths518）");
                    if (index>=0){
                        content = content.substring(0 ,index);}
                    appNewsDetailed.setContent(content);
                    appNewsDetailedService.updateAppNewsDetailed(appNewsDetailed);}
            }

        }
        return "";
    }
}
